﻿/**
 * 请不要将该类库用于非法途径和商业途径，与此产生的一切法律后果与本人无关。
 * 有任何问题，可以访问我的博客：http://baiyafeng.com 留言
 * 使用本类库请遵循GPL v3协议。
 **/

using System;
using System.Collections.Generic;
 
using System.Text;
using System.Data;
using System.Xml;
using System.Text.RegularExpressions;

namespace SinaBlogQiangShafa
{
   public class Job
    {
       private static readonly string domain = @"http://blog.sina.com.cn/rss/";
       private static readonly string mobile = System.Configuration.ConfigurationSettings.AppSettings["mobile"];
 
       private static readonly string url_pattern = @"s/blog_\w+\.html";
      
       private MatchCollection mc = null;
       private LinkedList<string> stack = new LinkedList<string>();
      
       public Job(string title)
       {
          
           _title = title;
           Isfirst();
       }
       private string _title = null;
       private bool _first = false;
       
       public string Title
       {
           get { return _title; }
           
       }
       
       private void  Isfirst()
       {
           DataAccess da = new DataAccess();
           stack = da.GetAll(_title);
           if ((stack == null) || (stack.Count == 0))
           {
               _first= true;
           }
           
           
       }
       public void Run()
       {
           DataAccess da = new DataAccess();
           DuanUrl du = new DuanUrl();
            RssGet rg = new RssGet();
           string address = domain + _title + ".xml" + DateTime.Now.Ticks.ToString();
         //  Log.WriteInfo(string.Format("[{0}]:{1}", DateTime.Now.ToString(), "请求的rss地址是" + address));
           string reContent = rg.GetRes(address);
           if (rg.Success)
           {
               Regex regex = new Regex(url_pattern);
               mc = regex.Matches(reContent, 0);




               if (_first)
               {
                   try
                   {

                       stack = new LinkedList<string>();


                       for (int i = 0; i < mc.Count; i++)
                       {

                           string url = mc[i].Value;

                           if (!stack.Contains(url))
                           {
                               stack.AddFirst(url);
                           }

                       }
                   }
                   catch (Exception error)
                   {
                       Log.WriteError(string.Format("[{0}]:{1}", DateTime.Now.ToString(), error.ToString()));
                   }

                   //更新数据库
                   da.Update_DB(_title, stack);
                   _first = false;
                   Log.WriteInfo(string.Format("[{0}]:{1}", DateTime.Now.ToString(), _title + "数据库更新完毕"));
               }
               else
               {

                   //更新Stack

                   string url = mc[0].Value;
                   if ((mc.Count > 0) && (Is_Newer(url)))
                   {
                       stack.RemoveLast();
                       stack.AddBefore(stack.First, url);

                       //发送通知
                       

                           string returnRss = @"http://blog.sina.com.cn/" + url + "#post";
                           string reUrl = du.BianDuan(returnRss); //得到短地址
                           Log.WriteInfo(string.Format("[{0}]:{1}", DateTime.Now.ToString(), _title + "更新:" + reUrl));
                          
                           if (du.Success)
                           {
                                 SmsFoFetion.sendMessage(_title + "更新:" + reUrl);

                           }
                           else
                           {
                                SmsFoFetion.sendMessage(_title + "更新:" + returnRss); ;
                           }

                       
                      


                       //更新数据库
                       da.Update_DB(_title, stack);
                       Log.WriteInfo(string.Format("[{0}]:{1}", DateTime.Now.ToString(), _title + "数据库更新完毕"));

                   }
                   
               }
           }
           else
           {
               Log.WriteInfo(string.Format("[{0}]:{1}", DateTime.Now.ToString(), _title + "连接超时"));
           }
       }
       protected bool Is_Newer(string url)
       {
           if (!Is_Exists(url))
               return true;
           return false;
       }
       protected bool Is_Exists(string url)
       {
           return stack.Contains(url);

       }
    }
}
